//A customized class for displaying the Template Column

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Orbitone.MeetingRooms.RoomFinder.UserControls.GridViewModel;

namespace Orbitone.MeetingRooms.RoomFinder.UserControls.GridView
{
    public class GridViewTemplate : ITemplate
    {
        //A variable to hold the type of ListItemType.

        //A variable to hold the column name.
        private readonly string _columnName;
        private readonly ListItemType _templateType;

        //Constructor where we define the template type and column name.
        public GridViewTemplate(ListItemType type, string colname)
        {
            //Stores the template type.
            _templateType = type;

            //Stores the column name.
            _columnName = colname;
        }

        #region ITemplate Members

        void ITemplate.InstantiateIn(Control container)
        {
            switch (_templateType)
            {
                case ListItemType.Header:
                    //Creates a new label control and add it to the container.
                    var lbl = new Label(); //Allocates the new label object.
                    lbl.ID = "Rowspan";
                    lbl.Text = _columnName; //Assigns the name of the column in the lable.
                    container.Controls.Add(lbl); //Adds the newly created label control to the container.
                    break;

                case ListItemType.Item:
                    var rowspan = new Label();
                    rowspan.Visible = false;
                    rowspan.DataBinding += RowspanDataBinding;
                    container.Controls.Add(rowspan); //Adds the newly created textbox to the container.

                    //Creates a new text box control and add it to the container.
                    var lb = new LinkButton(); //Allocates the new text box object.
                    lb.DataBinding += LinkbuttonDataBinding; //Attaches the data binding event.
                    lb.Click += CalendarItemClick;
                    container.Controls.Add(lb);

                    var lbl1 = new Label();
                    lbl1.DataBinding += LabelDataBinding;
                    container.Controls.Add(lbl1); //Adds the newly created textbox to the container.
                    break;

                case ListItemType.EditItem:
                    //As, I am not using any EditItem, I didnot added any code here.
                    break;

                case ListItemType.Footer:
                    var chkColumn = new CheckBox();
                    chkColumn.ID = "Chk" + _columnName;
                    container.Controls.Add(chkColumn);
                    break;
            }
        }

        #endregion

        public event EventHandler CalendarItemClick;

        private void RowspanDataBinding(object sender, EventArgs e)
        {
            var rowspan = (Label) sender;
            var container = (GridViewRow) rowspan.NamingContainer;
            var dataValue = DataBinder.Eval(container.DataItem, _columnName) as IGridViewItem;
            if (dataValue != null)
            {
                //hidden items will have rowspan 0
                rowspan.Text = dataValue.Rowspan.ToString();
            }
        }

        private void LabelDataBinding(object sender, EventArgs e)
        {
            var label = (Label) sender;
            var container = (GridViewRow) label.NamingContainer;
            var dataValue = DataBinder.Eval(container.DataItem, _columnName) as IGridViewItem;
            if (dataValue != null)
            {
                if (dataValue.GetType().Equals(typeof (GridViewLabelItem)))
                {
                    var item = dataValue as GridViewLabelItem;
                    label.Text = item.Label;
                    label.Attributes["title"] = item.Title;
                    if (!string.IsNullOrEmpty(item.Title))
                    {
                        label.CssClass = "calendaritem";
                    }
                }
            }
        }

        private void LinkbuttonDataBinding(object sender, EventArgs e)
        {
            var linkButton = (LinkButton) sender;
            var container = (GridViewRow) linkButton.NamingContainer;
            var dataValue = DataBinder.Eval(container.DataItem, _columnName) as IGridViewItem;
            if (dataValue != null)
            {
                if (dataValue.GetType().Equals(typeof (GridViewLinkItem)))
                {
                    var item = dataValue as GridViewLinkItem;
                    linkButton.Text = item.Label;
                    linkButton.CommandArgument = item.CommandArgument;
                    linkButton.CommandName = item.CommandName;
                    linkButton.Attributes["title"] = item.Title;
                    if (!string.IsNullOrEmpty(item.Title))
                    {
                        linkButton.CssClass = "calendaritem";
                    }
                }
            }
        }
    }
}